package com.zb.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zb.Service.DepartmentService;
import com.zb.Service.FiletableService;
import com.zb.Service.RecordService;
import com.zb.Service.UserService;
import com.zb.entity.Department;
import com.zb.entity.Filetable;
import com.zb.entity.Filetype;
import com.zb.entity.User;
import com.zb.util.CCRDFile;

@Scope("prototype")
@Component
@RequestMapping("/dept")
public class DepartmentController{
	@Resource
	private DepartmentService ds;
	@Resource
	private UserService us;
	private ServletContext servletContext;
	@Autowired
	private FiletableService fts;
	public void setServletContext(ServletContext context) {
		this.servletContext  = context;
	}
	
	@RequestMapping(params ="op=showRealName")
	@ResponseBody
	public List<User> toDept(int id){
		Department dept = ds.getDeptById(id);
		User u = us.getUserById(dept.getUser().getId());
		List<User> ulist = new ArrayList<User>();
		ulist.add(u);
		return ulist;
	}

	@ResponseBody
	@RequestMapping(params = "op=dtree")
	//得到组织结构的树形对象
	public List<Department> showTree(ModelMap modelMap){
		List<Department> dlist = ds.getTree();
		return dlist;
	}
	//进行修改的显示
	@RequestMapping(params = "op=getinfo")
	public String getInfo(int id,ModelMap modelMap){
		Department dept = ds.getDeptById(id);
		modelMap.put("dept", dept);
		return "deptshow";
	}
	/**
	 * 去添加页面
	 * @param id 点击treeview时的id
	 * @param isroot 是否为根节点 0 否 1 是
	 */
	@RequestMapping(params = "op=toadd")
	public String toadd(Integer id,String pdeptname,Integer isroot,ModelMap modelMap){
		modelMap.put("secid", id);
		modelMap.put("isroot", isroot);
		Department dept = new Department();
		Department pdept = new Department();
		pdept.setDeptname(pdeptname);
		pdept.setId(id);
		dept.setPerdept(pdept);
		modelMap.put("dept", dept);
		return "deptshow";
	}
	@RequestMapping(params = "op=update")
	public String doUpdate(Department dept,ModelMap modelMap,String uid ,Integer hid){
		if(uid!=null){
			if(isNum(uid)){
				dept.setUser(new User(Integer.parseInt(uid)));
			}else{
				dept.setUser(new User(hid));
			}
		}
		if(dept.getPerdept()==null){
			ds.updateRootNode(dept);
		}else{
			ds.updateChildNode(dept);
		}
		return "success";
	}
	@RequestMapping(params = "op=doadd")
	public String doAdd(HttpSession session, Department dept,ModelMap modelMap,String uid ,Integer hid){
		if(uid!=null){
			dept.setUser(new User(Integer.parseInt(uid)));
		}
		if(dept.getPerdept()==null){
			ds.addRootNode(dept);
		}else{
			
			//添加子部门时同时为其创建相应的文件夹
			Filetable ft = new Filetable();
			ft.setSecid(0);
			ft.setName(dept.getDeptname());
			ft.setFiletype(new Filetype(-1));
			ft.setUser(new User(1));
			ft.setDepartment(dept);
			ft.setPath("/"+dept.getDeptname()+"/");
			ft.setTip("备注");
			String realpath = this.servletContext.getRealPath("/文档管理"+ft.getPath());
			if(CCRDFile.createDir(realpath)){
				ds.addChildNode(dept);
				fts.addFile(ft);
			}else{

			}
		}
		User user = (User)session.getAttribute("sys_user");
		rs.addRecord(user.getId(), "添加"+dept.getDeptname()+"部门");
		return "success";
	}
	@Autowired
	private RecordService rs;
	@RequestMapping(params = "op=dodelete")
	public String doDelete(HttpSession session,Integer id,ModelMap modelMap){
		String msg="";
		if(ds.hasChild(id)>0){
			msg="该节点还有子节点不能删除";
			modelMap.put("msg", msg);
			return "error";
		}
		fts.delectFlieByDeptId(id);
		ds.deleteNode(id);
		User user = (User)session.getAttribute("sys_user");
		rs.addRecord(user.getId(), "删除"+"部门");
		return "success";
	}
	
	public boolean isNum(String str){
		for (int i = 0; i < str.length(); i++) {
			if(!Character.isDigit(str.charAt(i))){
				return false;
			}
		}
		return true;
	}

	

}

